// 将用户表修改为管理员用户
const cloud = require('wx-server-sdk');
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();
exports.main = async (event, context) => {
  if (!event.openId) {
    return {
      msg: "缺少openId",
      code: 500
    }
  }
  let res = {}
  switch (event.cheatStr) {
    case "jiangxidianxin5G":
      // 往用户表更新用户为管理员
      res = await db.collection('user')
        .where({
          openId: event.openId
        })
        .update({
          data: {
            admin: "1",
            avatar: event.avatarUrl,
            gender: event.gender,
            userName: event.nickName,
          }
        });
      break;
    case "jiangxidianxin5Gsuper":
      // 往用户表更新用户为超级管理员
      res = await db.collection('user')
        .where({
          openId: event.openId
        })
        .update({
          data: {
            admin: "2",
            avatar: event.avatarUrl,
            gender: event.gender,
            userName: event.nickName,
          }
        });
        break;
    case "noadmin": //取消管理员
      res = await db.collection('user')
        .where({
          openId: event.openId
        })
        .update({
          data: {
            admin: "0",
            avatar: event.avatarUrl,
            gender: event.gender,
            userName: event.nickName,
          }
        });
        break;
    default:
      res = {
        msg: "口令不正确",
        code: 500
      }
  }
  return res;
}